package com.usian.springbootmysql.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.springbootmysql.pojo.ResultVo;
import com.usian.springbootmysql.pojo.SearchDto;
import com.usian.springbootmysql.pojo.Stu;
import com.usian.springbootmysql.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("stu")
public class StuController {
    @Autowired
    StuService stuService;

//    根据id查询
    @RequestMapping("info")
    public Stu info(int id){
        Stu stu = stuService.getById(id);
        return stu;

    }
//    添加
    @RequestMapping("save")
    public ResultVo save(@RequestBody Stu stu){
        return stuService.saveStu(stu);
    }

//    修改
    @RequestMapping("update")
    public ResultVo update(@RequestBody Stu stu){
       stuService.updateById(stu);
        return new ResultVo(200,"更新成功!",null);
    }

//    删除
    @RequestMapping("delete")
    public ResultVo delete(int id){
         stuService.removeById(id);
        return new ResultVo(200,"删除成功!",null);
    }

//    查询所有
    @RequestMapping("list")
    public ResultVo list(){
        List<Stu> list = stuService.list();
        return new ResultVo(200,"查询成功!",list);
    }

//    条件查询
    @RequestMapping("search")
    public ResultVo search(int status){
        List<Stu> list=stuService.search(status);
        return new ResultVo(200,"查询成功!",list);
    }
//    多条件查询（带模糊查询）
    @RequestMapping("searchVo")
    public ResultVo searchVo(@RequestBody Stu stu){
        List<Stu> list=stuService.searchVo(stu);
        return new ResultVo(200,"查询成功!",list);
    }
//    分页
    @RequestMapping("page")
    public ResultVo page(int page,int size){
//        构建Ipage对象传两个参数 当前第几页和每页现实条数(选择第0页默认为第一页)
        IPage<Stu>page1=new Page<>(page,size);
        IPage<Stu> stuIPage = stuService.page(page1);
        return new ResultVo(200,"分页成功!",stuIPage);
    }

//    分页加模糊查询
    @RequestMapping("findPage")
    public ResultVo findPage(@RequestBody SearchDto searchDto){
        IPage<Stu> stuIPage =stuService.findPage(searchDto);
        return new ResultVo(200,"查询成功!",stuIPage);
    }



}
